package com.qifenqian.turbot.module.ui.staff

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.module.adapter.restaurant.StaffManageAdapter
import com.qifenqian.turbot.module.contract.StaffContract
import com.qifenqian.turbot.module.entity.response.StaffEntity
import com.qifenqian.turbot.module.presenter.StaffPresenter
import kotlinx.android.synthetic.main.activity_staff_manage.*
import kotlinx.android.synthetic.main.dialog_edit_text_2.view.*
import org.jetbrains.anko.alert
import org.jetbrains.anko.toast

private const val TYPE_STAFF_ADD = 1    //添加
private const val TYPE_STAFF_EDIT = 2   //编辑

/**
 * @author H
 * @createDate 2018/12/27 14:15
 * @description 店员管理  // TODO: 2019/1/9 增删之后的处理可优化
 * @see
 * @since
 */
class StaffManageActivity : BaseActivity<StaffPresenter, StaffContract.View>(), StaffContract.View {

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, StaffManageActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int {
        return R.layout.activity_staff_manage
    }

    override fun bindPresenter(): StaffPresenter? {
        return StaffPresenter(this, this)
    }

    private var mStaffEntities: MutableList<StaffEntity> = mutableListOf()
    private var mStaffManageAdapter: StaffManageAdapter? = null

    override fun initial() {
        mStaffManageAdapter = StaffManageAdapter(mStaffEntities)
        mPresenter!!.getStaffList()
        rv_staff_manage.layoutManager = LinearLayoutManager(this)
        rv_staff_manage.adapter = mStaffManageAdapter
        btn_staff_manage_add.setOnClickListener {
            showStaffDialog(TYPE_STAFF_ADD)
        }

        mStaffManageAdapter!!.setOnChildClickListener(object : StaffManageAdapter.OnItemChildClickListener {
            override fun onEditClick(position: Int) {
                // 2018/12/20 弹出添加店员的对话框 ----->
                showStaffDialog(TYPE_STAFF_EDIT, position)
            }

            override fun onDeleteClick(position: Int) {
                // 2018/12/20 移除店员的对话框 ----->
                alert("确认移除该店员吗?") {
                    positiveButton("确定") {
                        // 2018/12/20 移除该店员 ----->
                        mPresenter!!.deleteStaff(mStaffEntities[position].id)
                    }
                    negativeButton("取消") {
                        it.dismiss()
                    }
                }.show()
            }

        })
    }

    /**
     * 创建对话框 1添加 2编辑
     */
    private var mDialog: Dialog? = null

    private fun showStaffDialog(type: Int, position: Int = -1) {
        mDialog = Dialog(this)
        val view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text_2, null)
        if (type == TYPE_STAFF_ADD) {
            view.tv_dialog_edit_title.text = "添加店员"
            view.et_dialog_edit_content1.hint = "请输入姓名"
            view.et_dialog_edit_content2.hint = "请输入手机号"
        } else if (type == TYPE_STAFF_EDIT) {
            view.tv_dialog_edit_title.text = "编辑店员"
            view.et_dialog_edit_content1.setText(mStaffEntities[position].userName)
            view.et_dialog_edit_content2.setText(mStaffEntities[position].userCode)
        }
        mDialog!!.setContentView(view)
        view.btn_dialog_edit_cancel.setOnClickListener { mDialog!!.dismiss() }
        view.btn_dialog_edit_ok.setOnClickListener {
            if (view.et_dialog_edit_content1.text.toString().isEmpty()) {
                toast("店员名字不能为空")
                return@setOnClickListener
            }
            if (view.et_dialog_edit_content2.text.toString().isEmpty()) {
                toast("店员电话不能为空")
                return@setOnClickListener
            }
            // 2018/12/19  添加店员 ----->
            if (type == TYPE_STAFF_ADD) {
                mPresenter!!.addStaff(view.et_dialog_edit_content1.text.toString(), view.et_dialog_edit_content2.text.toString())
            } else {
                mPresenter!!.editStaff(mStaffEntities[position].id, view.et_dialog_edit_content1.text.toString(), view.et_dialog_edit_content2.text.toString())
            }
        }
        mDialog!!.show()
    }

    override fun getStaffListResult(isSuccess: Boolean, code: Int, msg: String?, staffEntities: List<StaffEntity>?) {
        if (isSuccess) {
            mStaffEntities.clear()
            mStaffEntities.addAll(staffEntities!!)
            mStaffManageAdapter!!.notifyDataSetChanged()
        } else {
            toast(msg!!)
            finish()
        }
    }

    override fun addStaffResult(isSuccess: Boolean, code: Int, msg: String?, response: Double) {
        if (isSuccess) {
            mPresenter!!.getStaffList()
        }
        if (mDialog != null && mDialog!!.isShowing) {
            mDialog!!.dismiss()
        }
        toast(msg!!)
    }

    override fun editStaffResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            mPresenter!!.getStaffList()
            if (mDialog != null && mDialog!!.isShowing) {
                mDialog!!.dismiss()
            }
        }
        toast(msg!!)
    }

    override fun deleteStaffResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            mPresenter!!.getStaffList()
//            mStaffManageAdapter.remove(position)
//            mStaffManageAdapter.notifyDataSetChanged()
        }
        toast(msg!!)
    }

}
